/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lector;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author nexo__000
 */
public class lector {

    public static void printFicha() {
    }

    public static void leer() {
        leer("fuerza");
        leer("destreza");
        leer("inteligencia");
    }

    public static int leerAtributoRaza(String atributo, String raza) {
        int resultado = 0;
        try {
            SAXBuilder builder = new SAXBuilder();
            File xmlFile = new File("./razas.xml");


            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();
            int posicion = buscarEtiqueta(rootNode, raza);
            if (posicion != -1) {
                Element node = (Element) list.get(posicion);
                resultado = Integer.parseInt(node.getChildText(atributo));
            }
        } catch (JDOMException | IOException ex) {
            Logger.getLogger(lector.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultado;
    }

    private static int buscarEtiqueta(Element pajar, String aguja) {
        int posicion = -1;
        List lista = pajar.getChildren();
        for (int i = 0; i < lista.size(); i++) {
            Element node = (Element) lista.get(i);
            if (node.getName().equals(aguja)) {
                posicion = i;
            }
        }
        return posicion;
    }

    public static void leer(String atributo) {

        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("./ejemplo2.xml");


        try {

            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();
            System.out.print("\nLa " + atributo + " del personaje es: ");
            int suma = 0;
            int valor = 0;
            for (int i = 0; i < list.size(); i++) {
                Element node = (Element) list.get(i);
                System.out.print(node.getName());
                if (node.getName().equals("raza")) {
                    valor = leerAtributoRaza(atributo, getRaza(node));
                    System.out.print(" (" + getRaza(node) + ") ");
                } else {
                    valor = Integer.parseInt(node.getChildText(atributo));
                }
                System.out.print(": ");
                System.out.print(valor);
                suma += valor;
                if (i < list.size() - 1) {
                    System.out.print(" + ");
                } else {
                    System.out.print(" = " + suma);
                }

//		   System.out.println("First Name : " + node.getChildText("firstname"));
//		   System.out.println("Last Name : " + node.getChildText("lastname"));
//		   System.out.println("Nick Name : " + node.getChildText("nickname"));
//		   System.out.println("Salary : " + node.getChildText("salary"));

            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }
    }
    
    private static String getRaza(Element pajar){
        List lista = pajar.getChildren();
        Element node = (Element)lista.get(0);
        return node.getName();
        
    }
    
    private static Element getHijoInmediato(Element padre){
        List lista = padre.getChildren();
        return (Element)lista.get(0);
    }
}
